package com.yuanheng100.ems4j.mapper;

import com.yuanheng100.ems4j.model.Ems4jDepartment;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface Ems4jDepartmentMapper
{

    /**
     * 根据部门id查询部门
     *
     * @param id
     * @return
     */
    @Select({
            "SELECT",
            "id, parentid, name, ordernum, appids, depstatus, deptype, phoneareacode, telephone, ",
            "regioncode, createtime, address, deletedate, dingextend, wexinextend",
            "FROM ems4j_department",
            "WHERE id = #{id,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "parentid", property = "parentid", jdbcType = JdbcType.INTEGER),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ordernum", property = "ordernum", jdbcType = JdbcType.SMALLINT),
            @Result(column = "appids", property = "appids", jdbcType = JdbcType.VARCHAR),
            @Result(column = "depstatus", property = "depstatus", jdbcType = JdbcType.SMALLINT),
            @Result(column = "deptype", property = "deptype", jdbcType = JdbcType.SMALLINT),
            @Result(column = "phoneareacode", property = "phoneareacode", jdbcType = JdbcType.SMALLINT),
            @Result(column = "telephone", property = "telephone", jdbcType = JdbcType.BIGINT),
            @Result(column = "regioncode", property = "regioncode", jdbcType = JdbcType.INTEGER),
            @Result(column = "createtime", property = "createtime", jdbcType = JdbcType.DATE),
            @Result(column = "address", property = "address", jdbcType = JdbcType.VARCHAR),
            @Result(column = "deletedate", property = "deletedate", jdbcType = JdbcType.DATE),
            @Result(column = "dingextend", property = "dingextend", jdbcType = JdbcType.VARCHAR),
            @Result(column = "wexinextend", property = "wexinextend", jdbcType = JdbcType.VARCHAR)
    })
    Ems4jDepartment selectByPrimaryKey(Integer id);

    /**
     * 根据parentid查询子部门
     *
     * @param parentId
     * @param deptype
     * @return
     */
    @SelectProvider(type = Ems4jDepartmentSQLProvider.class, method = "selectByParentidAndDeptype")
    List<Ems4jDepartment> selectByParentidAndDeptype(@Param("parentid") int parentId, @Param("deptype") short deptype);

    /**
     * 根据子部门ID获取父部门
     *
     * @param depId
     * @return
     */
    @Select({
            "SELECT",
            "id, parentid, name, ordernum, appids, depstatus, deptype, phoneareacode, telephone, ",
            "regioncode, createtime, address, deletedate, dingextend, wexinextend",
            "FROM ems4j_department",
            "WHERE parentid = (SELECT parentid FROM ems4j_department WHERE id = #{depId,jdbcType=INTEGER})"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "parentid", property = "parentid", jdbcType = JdbcType.INTEGER),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ordernum", property = "ordernum", jdbcType = JdbcType.SMALLINT),
            @Result(column = "appids", property = "appids", jdbcType = JdbcType.VARCHAR),
            @Result(column = "depstatus", property = "depstatus", jdbcType = JdbcType.SMALLINT),
            @Result(column = "deptype", property = "deptype", jdbcType = JdbcType.SMALLINT),
            @Result(column = "phoneareacode", property = "phoneareacode", jdbcType = JdbcType.SMALLINT),
            @Result(column = "telephone", property = "telephone", jdbcType = JdbcType.BIGINT),
            @Result(column = "regioncode", property = "regioncode", jdbcType = JdbcType.INTEGER),
            @Result(column = "createtime", property = "createtime", jdbcType = JdbcType.DATE),
            @Result(column = "address", property = "address", jdbcType = JdbcType.VARCHAR),
            @Result(column = "deletedate", property = "deletedate", jdbcType = JdbcType.DATE),
            @Result(column = "dingextend", property = "dingextend", jdbcType = JdbcType.VARCHAR),
            @Result(column = "wexinextend", property = "wexinextend", jdbcType = JdbcType.VARCHAR)
    })
    Ems4jDepartment selectParentByPrimaryKey(int depId);

    /**
     * 查询根部门
     *
     * @return
     */
    @Select({
            "SELECT",
            "id, parentid, name, ordernum, appids, depstatus, deptype, phoneareacode, telephone, ",
            "regioncode, createtime, address, deletedate, dingextend, wexinextend",
            "FROM ems4j_department",
            "WHERE parentid IS NULL OR parentid = id"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "parentid", property = "parentid", jdbcType = JdbcType.INTEGER),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "ordernum", property = "ordernum", jdbcType = JdbcType.SMALLINT),
            @Result(column = "appids", property = "appids", jdbcType = JdbcType.VARCHAR),
            @Result(column = "depstatus", property = "depstatus", jdbcType = JdbcType.SMALLINT),
            @Result(column = "deptype", property = "deptype", jdbcType = JdbcType.SMALLINT),
            @Result(column = "phoneareacode", property = "phoneareacode", jdbcType = JdbcType.SMALLINT),
            @Result(column = "telephone", property = "telephone", jdbcType = JdbcType.BIGINT),
            @Result(column = "regioncode", property = "regioncode", jdbcType = JdbcType.INTEGER),
            @Result(column = "createtime", property = "createtime", jdbcType = JdbcType.DATE),
            @Result(column = "address", property = "address", jdbcType = JdbcType.VARCHAR),
            @Result(column = "deletedate", property = "deletedate", jdbcType = JdbcType.DATE),
            @Result(column = "dingextend", property = "dingextend", jdbcType = JdbcType.VARCHAR),
            @Result(column = "wexinextend", property = "wexinextend", jdbcType = JdbcType.VARCHAR)
    })
    List<Ems4jDepartment> selectRootDepartment();

    @Insert({
            "INSERT INTO ems4j_department (id, parentid, ",
            "name, ordernum, ",
            "appids, depstatus, ",
            "deptype, phoneareacode, ",
            "telephone, regioncode, ",
            "createtime, address, ",
            "deletedate, dingextend, ",
            "wexinextend)",
            "VALUES (#{id,jdbcType=INTEGER}, #{parentid,jdbcType=INTEGER}, ",
            "#{name,jdbcType=VARCHAR}, #{ordernum,jdbcType=SMALLINT}, ",
            "#{appids,jdbcType=VARCHAR}, #{depstatus,jdbcType=SMALLINT}, ",
            "#{deptype,jdbcType=SMALLINT}, #{phoneareacode,jdbcType=SMALLINT}, ",
            "#{telephone,jdbcType=BIGINT}, #{regioncode,jdbcType=INTEGER}, ",
            "#{createtime,jdbcType=DATE}, #{address,jdbcType=VARCHAR}, ",
            "#{deletedate,jdbcType=DATE}, #{dingextend,jdbcType=VARCHAR}, ",
            "#{wexinextend,jdbcType=VARCHAR})"
    })
    int insert(Ems4jDepartment record);


    @Update({
            "UPDATE ems4j_department",
            "SET parentid = #{parentid,jdbcType=INTEGER},",
            "name = #{name,jdbcType=VARCHAR},",
            "ordernum = #{ordernum,jdbcType=SMALLINT},",
            "appids = #{appids,jdbcType=VARCHAR},",
            "depstatus = #{depstatus,jdbcType=SMALLINT},",
            "deptype = #{deptype,jdbcType=SMALLINT},",
            "phoneareacode = #{phoneareacode,jdbcType=SMALLINT},",
            "telephone = #{telephone,jdbcType=BIGINT},",
            "regioncode = #{regioncode,jdbcType=INTEGER},",
            "createtime = #{createtime,jdbcType=DATE},",
            "address = #{address,jdbcType=VARCHAR},",
            "deletedate = #{deletedate,jdbcType=DATE},",
            "dingextend = #{dingextend,jdbcType=VARCHAR},",
            "wexinextend = #{wexinextend,jdbcType=VARCHAR}",
            "WHERE id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Ems4jDepartment record);
}